module MC.View {
	export class Search {
		public constructor() {
			this.lishi();
			this.init();
		}
		private type = 'gs';
		private word = '';
		private lishi() {
			let list = MC.Cache.getObj('search');
			if (list) {
				$('#searchHistory').show();
				for (let i = 0; i < list.length; i++) {
					let li = `<li>` + list[i] + `</li>`;
					$('#historyList').append(li);
				}
			} else {
				$('#searchHistory').hide();
			}
		}
		private init() {
			let mthis = this;
			$('#searchTitle .findButton').on('click', () => {
				this.findclick();
			});
			$('#searchPage .topUl .li').on('click', function () {
				let li = $(this);
				$('#searchPage .topUl .li').removeClass('on');
				li.addClass('on');
				mthis.type = li.attr('s_type');
				mthis.loadData();
			});
			$('#historyList').on('click','li',function(){
				let word=$(this).text();
				$('#word').val(word);
				mthis.findclick();
			});
		}
		private findclick() {
			let word = $('#word').val();
			if (word == '') {
				MC.msg.show("必须输入关键词");
				return;
			}
			if (word.length >= 10) {
				MC.msg.show("输入的关键词太长了,不能超过10个字符");
				return;
			}
			this.word = word;
			this.data=[];
			this.loadData();
			this.addLishi(this.word);
		}
		private addLishi(word:string){
			let list = MC.Cache.getObj('search');
			if (!list) {
				 list=[];
			}
			if(list[0]&&list[0]==word){
				return;
			}
			///list.push(word);
			let newlist=[];
			newlist.push(word);
			for(let i=0;i<list.length&&i<16;i++){
				if(newlist.indexOf(list[i])==-1){
					newlist.push(list[i]);
				} 
			}
			MC.Cache.setObj('search',newlist);
		}





		private data: any = [];
		/*** 加载指定类型的数据 */
		private loadData() {
			$('.contentDiv .conDiv,#searchHistory').hide();//,#searchPage .topUlDiv
			if (this.data[this.type]) {
				$('.contentDiv .conDiv' + '.c' + this.type).show();
				return;
			}
			MC.apiD('search_word', { word: this.word, type: this.type }, (data: any) => {
				this.data[this.type] = data;
				$('#searchPage .topUlDiv').show();
				let comdiv = $('.contentDiv .conDiv' + '.c' + this.type);
				if (!data || data.length == 0) {
					comdiv.html(this.nullData);
				} else {
					comdiv.html(this.listToView(data));
				}
				comdiv.show();
				///	console.log(data);
			});
		}

		/** 转换为视图 */
		private listToView(list: any) {
			if (this.type == 'gs') {
				let html = '';
				for (let i = 0; i < list.length; i++) {
					let data = list[i];
					html += `<li class="li clearfix">
                    <img class="head" src="`+ data.user_head + `" />
                    <a class="title_desc" href="/h5/index/comShow.html?uid=`+ data.uid + `">
                        <span class="title">`+ data.com_name + `</span>
                        <p class="desc">`+ data.detail + `</p>
                    </a>
                </li> `;
				}
				return html;
			} else if (this.type == 'gc') {
				let html = '';
				for (let i = 0; i < list.length; i++) {
					let data = list[i];
					html += `<li class="li clearfix">
                    <img class="head" src="`+ data.user_head + `" />
                    <a class="title_desc" href="/h5/index/facShow.html?uid=`+ data.uid + `">
                        <span class="title">`+ data.com_name + `</span>
                        <p class="desc">`+ data.detail + `</p>
                    </a>
                </li> `;
				}
				return html;
			}else if (this.type == 'gr') {
				let html = '';
				for (let i = 0; i < list.length; i++) {
					let data = list[i];
					let womanico='';
					if(data.sex==MC.DEL.UserModel.Sex_Nv){
						womanico='womanico';
					}
					html += `<li class="li clearfix">
                <img class="head" src="`+ data.user_head + `" />
                <a class="title_desc" href="/h5/index/staffShow.html?id=`+ data.uid + `">
                    <p class="title"><span class="titlename">`+ data.user_name + `</span>
                        <i class="sexIco `+womanico+`"></i></p>
                    <p class="desc">电话: `+ data.phone + `</p>
                </a>
            </li>`;
				}
				return html;
			}else if (this.type == 'hd') {
				let html = '';
				for (let i = 0; i < list.length; i++) {
					let data = list[i];
					let is_rob='';
					if(data.is_rob==MC.DEL.OrderBaleModel.is_rob_qiangdan){
						is_rob='qiangdanIco';
					}
					html += `
						<li class="li clearfix">
						<div class="headDiv">
							<img class="head" src="`+data.img+`" />
						</div>
						<a class="title_desc" href="/h5/order/orderShow.html?id=`+ data.id + `">
							<p class="title"><span class="titlename">`+ data.name + `</span>
								<i class="is_robIco `+is_rob+`"></i></p>
							<p class="detail">`+ data.name + `</p>
							<p class="period">到期:`+ data.period + `</p>
						</a>
					</li>
					`;
				}
				return html;
			}else if (this.type == 'gw') {///岗位
				let html = '';
				for (let i = 0; i < list.length; i++) {
					let data = list[i]; 
					html += `
						<li class="li clearfix">
						<div class="headDiv">
							<img class="head" src="`+data.user_head+`" />
						</div>
						<a class="title_desc" href="/h5/index/job/jobShow.html?jobId=`+ data.id + `">
							<p class="title"><span class="company_name">`+ data.company_name + `</span> 
								<p class="emolument">¥`+ data.emolument + `</p>
							</p>
							<p class="job">`+ data.job + `</p>
							<p class="skill">`+ data.skill + `</p>
							<p class="address">`+ data.address + `</p>
						</a>
					</li>
					`;
				}
				return html;
			}


			else {
				return '';
			}

		}
		private nullData = `<div class="noData">
            <img src="/h5/images/baoqian.png" class="nodataImg"/>
            <p class="text">抱歉，没有找到符合的结果~</p>
        </div>`;
	}
}